
package com.fire.control.system.module.role.controller.web;

import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fire.control.system.entity.Resource;
import com.fire.control.system.entity.Role;
import com.fire.control.system.module.common.controller.BaseController;
import com.fire.control.system.module.role.service.IRoleService;
import com.fire.control.system.standard.BusinessInterfaceStandard;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.role.RoleRequestProtocol;
import com.fire.control.system.support.util.JSONUtil;

/**
 * @author liuxincheng
 * @ClassName: RoleControl
 * @Description: TODO(角色控制器)
 * @date 2017年6月22日 下午7:47:33
 */
@RestController
@RequestMapping(path = "/web/roleCtr")
public class RoleControl extends BaseController implements BusinessInterfaceStandard<RoleRequestProtocol, ResponseProtocol> {
	@Autowired
	private IRoleService roleService;

	/**
	 * @author liuxincheng
	 * @Title: saveRole
	 * @Description: TODO(获得当前登陆人员的角色和资源)
	 * @param @param requestProtocol
	 * @param @return 设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/getRoleAndResources", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getRoleAndResources(RoleRequestProtocol requestProtocol) {
		ResponseProtocol response = roleService.getRoleAndResources(requestProtocol,this.getUser());
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: saveRole
	 * @Description: TODO(添加角色)
	 * @param @param requestProtocol
	 * @param @return 设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/saveRole", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol saveRole(RoleRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = roleService.saveRole(requestProtocol, this.getUser());
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: roleDistributionResource
	 * @Description: TODO(角色分配权限)
	 * @param @param requestProtocol
	 * @param @param request
	 * @param @return 设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "/roleDistributionResource", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol roleDistributionResource(RoleRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = roleService.roleDistributionResource(requestProtocol);
		return response;
	}

	/**
	 * 删除角色
	 * @Title: removeRole
	 * @Description: TODO
	 * @param protocol
	 * @param request
	 * @return
	 * @return: ResponseProtocol
	 */
	@RequestMapping(path = "/removeRole", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol removeRole(RoleRequestProtocol protocol,HttpServletRequest request) {
		protocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = roleService.removeRole(protocol, request);
		return response;
	}

	/*****************************************角色管理辅助接口**********************************************************/
	/**
	 * 根据角色查询资源
	 * @Title: getRoleResources
	 * @Description: TODO
	 * @param protocol
	 * @param request
	 * @return
	 * @return: ResponseProtocol
	 */
	@RequestMapping(path = "/getRoleResources", method = RequestMethod.POST)
	@ResponseBody
	public Set<Resource> getRoleResources(RoleRequestProtocol protocol) {
		List<String> roleIds = JSONUtil.toList(protocol.getRoleId(), String.class);
		Set<Resource> resourceList = roleService.getResourceByRoleId(roleIds);
		return resourceList;
	}
	
	/**
	 * @author liuxincheng
	 * @Title: getAllRoles
	 * @Description: TODO(获取系统中所有角色)
	 * @param @return    设定文件
	 * @return List<Role>   返回类型
	 * @throws
	 */
	@RequestMapping(path = "/getAllRoles", method = RequestMethod.POST)
	@ResponseBody
	public List<Role> getAllRoles() {
		List<Role> roleList = roleService.getAllRoles();
		return roleList;
	}
}
